// pages/freereg/free1/free1.js
var qcloud = require('../../../vendor/wafer2-client-sdk/index')
var config = require('../../../config')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'',
    userID:'',
    birthday:'',
    lastDate:'',
    buttonname:'获取验证码',
    buttonstat:false,
    user_obj:{
      name:'您的姓名',
      phoneNum:'手机号码',
      recod:'验证码',
      sex:'',
      birthday:'',
      nameFcous:false,
      buttDis:false,
      reged:false,
      address:'家庭地址'
    },
      userInfo: {},
      logged: false,
      takeSession: false,
      requestResult: ''

  
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var today=util.formatTime(new Date());  
      console.log('页面传递参数',options);
       this.setData({
    //     username:options.name,
        birthday:today,
        lastDate:today
      

     })
  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  newdata:function(e){
    var tmpbirday="user_obj.birthday";
    this.setData({
      birthday:e.detail.value,
      [tmpbirday]:e.detail.value
    })

  },



// 清除默认用户名
  clearValue:function(e){
     var that = this;
     var tmpName="user_obj.name";
     if (e.detail.value=="您的姓名") {
        that.setData({
        [tmpName]:''
    })
     }

  } ,

// 用户名无内容检测
  ceckValue:function(e){
    var inValue=e.detail.value;
    var that = this;
    var tmpName="user_obj.name";
    var tmpFoucs="user_obj.nameFcous";

    if (inValue=='') {
      that.setData({
        [tmpName]:"您的姓名",
        [tmpFoucs]:true

      })
    }
  },


  // 清除默认电话号码
  clearPnumValue:function(e){
     var that = this;
     var tmpPhnum="user_obj.phoneNum";
     if (e.detail.value=="手机号码") {
        that.setData({
        [tmpPhnum]:''
    })
     }

  } ,


// 无电话号码恢复默认
  ckPnumValue:function(e){
    var inValue=e.detail.value;
    var that = this;
    var tmpName="user_obj.phoneNum";

    if (inValue=='') {
      that.setData({
        [tmpName]:"手机号码",

      })
    }
  },




    // 清除默认验证码
  clearRecodValue:function(e){
     var that = this;
     var tmpPhnum="user_obj.recod";
      if (e.detail.value=="验证码") {
        that.setData({
        [tmpPhnum]:''
    })
     }

  } ,

// 验证码无内容恢复默认值
  ceckredValue:function(e){
    var inValue=e.detail.value;
    var that = this;
    var tmpName="user_obj.recod";
    if (inValue=='') {
      that.setData({
        [tmpName]:"验证码",

      })
    }
  },


// 提交按钮后收集form表单数据。
  submitinfo:function(e){
    // 检验验证码是否正确，如果正确，将当前用户信息赋值给全局变量并向服务器提交信息
    // getApp().globalData.guser_obj=null;
    //console.log("表单数据",e);
    var tmpName="user_obj.name";
    var tmpPhnum="user_obj.phoneNum";
    var tmpsex="user_obj.sex";
    var reged="user_obj.reged";
    var address="user_obj.address";
    var birthday="user_obj.birthday"
    var that = this;

    // 将当前用户赋值给全局变量用户模型
    //console.log(getApp().globalData.guser_obj);
    that.setData({
      [tmpName]:e.detail.value.name,
      [tmpPhnum]:e.detail.value.phoneNum,
      [tmpsex]:e.detail.value.sex,
      [reged]:true,
      [address]:e.detail.value.address,
      [birthday]:e.detail.value.birthday


    })
    getApp().globalData.guser_obj=that.data.user_obj;
    var userobj=that.data.user_obj;
    
   // 写入用户信息表
    this.inser(userobj)

   },


      inser:function (e) {
            console.log('当前数据',e);            
             var open = getApp().globalData.openid;
             wx.request({
               url:'https://evekykeb.qcloud.la/weapp/SoboDb/inser',
               method:'POST',
               header: {'content-type':'application/x-www-form-urlencoded'},
               data:{
                 sex:e.sex,
                 address:e.address,
                 birthday:e.birthday,
                 phoneNum:e.phoneNum,
                 oppenid:open,
                 nicname:e.name,
               },
               success:function (res) {
                 //console.log("用户信息登记信息成功",res.data[0].user_id);
                 wx.request({
                   url:'https://evekykeb.qcloud.la/weapp/SoboDb/inserpat',
                   method:'POST',
                   header: {'content-type':'application/x-www-form-urlencoded'},
                   data:{
                     
                     'pat_name':res.data[0].nicname,
                     'relation': '本人',
                     'birthday':res.data[0].birthday,
                     'sex':res.data[0].sex,
                     'from_id':res.data[0].user_id,
                     'from_op':res.data[0].oppenid,
                     'address':res.data[0].address
                   },

                   success:function (res1) {
                      wx.redirectTo({
                       url:'../free2/free2?oppenid='+res.data[0].oppenid +'&userid='+res.data[0].user_id,
                       success:function (reg1) {
                         console.log(reg1);
                       },
                       fail:function (err1) {
                         console.log(err1);
                       }
                     })
                   },

                   fail:function (err) {
                     // body...
                   },

             })
              },
              
               fail:function (error) {
                 console.log("登记信息错误",error);
               }

             })
          },





    login: function () {
        if (this.data.logged) return

        util.showBusy('正在登录')
        var that = this

        // 调用登录接口
        qcloud.login({
            success(result) {
                // console.log(result);
                if (result) {
                    util.showSuccess('登录成功');
                    that.setData({
                        userInfo: result,
                        logged: true
                    })
                    
                } else {
                    // 如果不是首次登录，不会返回用户信息，请求用户信息接口获取
                    qcloud.request({
                        url: config.service.requestUrl,
                        login: true,
                        success(result) {
                            util.showSuccess('登录成功')
                            that.setData({
                                userInfo: result.data.data,
                                logged: true
                            })
                        },

                        fail(error) {
                            util.showModel('请求失败', error)
                            console.log('request fail', error)
                        }
                    })
                }
            },

            fail(error) {
                util.showModel('登录失败', error)
                console.log('登录失败', error)
            }
        })
    },


    login1:function () {
      wx.login({
        success:function (res) {
          if (res.code) {
             console.log("本地登录成功，下一步向服务器进行静态登录");
             wx.request({
               url: 'https://evekykeb.qcloud.la/weapp/login',
               data: {
               code: res.code
              },
              success: function(res) {
                console.log("服务器登录成功");
                console.log(res);
              }

          })
             console.log(res); 
          }else{
            util.showModel('登录失败', error)
          }

        }

      })
        
    },

   qcloudin:function () {
      qcloud.setLoginUrl('https://evekykeb.qcloud.la/weapp/login');
      qcloud.login({
        success:function(res) {
          console.log("登录成功",res);
        },
        fail:function (res) {
          console.log("登录失败",res);
        }
      });
    },

    leron:function () {
        qcloud.request({
        login:true,
        url:'https://evekykeb.qcloud.la/weapp/SoboDb',
        method:'POST',
        header: {'content-type':'application/x-www-form-urlencoded'// 默认值
  },
        data:{
          name:'张三丰',
          address:'中华人民共和国',
          starr:{'num':13,'en':65,'cn':78}
        },
        success:function (res) {
          console.log(res);
        }
      })
    },



    ceckphone:function () {
     var that = this;
     var res = 60;
      this.setData({
        buttonname:90,
        buttonstat:true
      })
      var res=90;

      this.timer(90);
    },

    timer:function (res) {
      if (res <= 0 ) {
       clearTimeout(t)
        this.setData({
          buttonname:'再次获取',
          buttonstat:false
        })

      } 
      else {
       this.setData({
         buttonname:res+'秒后获取', 
         buttonstat:true
        })
       res = parseInt(res)-1
       var t = setTimeout(this.timer(res),1000);
      }

      // body...
    },









    // inser:function () {
    //   var open = getApp().globalData.openid;
    //   qcloud.request({
    //     login:true,
    //     url:'https://evekykeb.qcloud.la/weapp/SoboDb/inser',
    //     method:'POST',
    //     header: {'content-type':'application/x-www-form-urlencoded'},
    //     data:{
    //       sex:'女',
    //       address:'河南省安阳市持股三区一号楼5院',
    //       birthday:'2011-12-11',
    //       phoneNum:'13525260870',
    //       oppenid:open,
    //       nicname:'如冰'
    //     },
    //     success:function (res) {
    //       console.log(res);
    //     },
    //     fail:function (error) {
    //       console.log(error);
    //     }

    //   })
    // }



})